<style type="text/css" title="currentStyle">
	@import "../media/css/demo_page.css";
	@import "../media/css/demo_table_jui.css";
	@import "../examples_support/themes/smoothness/jquery-ui-1.8.4.custom.css";
	@import "media/css/TableTools_JUI.css";
</style>
<script type="text/javascript" charset="utf-8" src="../media/js/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8" src="media/js/ZeroClipboard.js"></script>
<script type="text/javascript" charset="utf-8" src="media/js/TableTools.js"></script>
<script type="text/javascript" charset="utf-8">
	$(document).ready( function () {
		$('#example').dataTable( {
			"bJQueryUI": true,
			"sPaginationType": "full_numbers",
			"sDom": 'Tlfrtip',
			"oTableTools": {
				"aButtons": [
					"copy", "csv", "xls", "pdf", "print",
					{
						"sExtends":    "collection",
						"sButtonText": "Save",
						"aButtons":    [ "csv", "xls", "pdf" ]
					}
				]
			}
		} );
	} );
</script>
<?php
function chk_folder($filename)
{ //echo $filename;
	$fp_load = @fopen($filename, "rb");
	if ( $fp_load )
	{ 
		return true;
	}
	else
	{ 
		return false;
	}
}


function gpspathFunAll($date_offline,$deviceIMEI)
{
	$path1=DATAPATH.date('d-m-Y',strtotime($date_offline))."/".$deviceIMEI.".txt";
	//$path1 = "http://localhost/src/352848025673738.txt";
	
	if(chk_folder($path1))
	{
		$file1 = @fopen($path1, "r");
		if($file1)
		{
			$i=0;
			while(!feof($file1))
			{
			  $data1= fgets($file1);				 
			}
			$data1 = getSortedData($data1);
			fclose($file1);
		}
		return $data1;
	}
		
}

function sec2hms($sec, $padHours=FALSE) {
    $hours_min_secs = "";
    $hours = INTVAL(INTVAL($sec) / 3600); 
    $hours_min_secs .= ($padHours) ? STR_PAD($hours, 2, "0", STR_PAD_LEFT). ':' : $hours. ':';

    $minutes = INTVAL(($sec / 60) % 60); 
   $hours_min_secs .= STR_PAD($minutes, 2, "0", STR_PAD_LEFT). ':';
    $seconds = INTVAL($sec % 60); 
    $hours_min_secs .= STR_PAD($seconds, 2, "0", STR_PAD_LEFT);
    return $hours_min_secs;
}

function addcolon($input){
  $tmp = '';
 for($i=0;$i<strlen($input);$i+=2){
  $tmp.=substr($input, $i, 2).':';
 }
 $input=substr($tmp, 0, strlen($tmp)-1);

 return $input;
}
function getClosestElement($array, $value) 
{
	$size = count($array);
	$tmp = -1;
	if ($size > 0) 
	{
		//echo max($array)."<br><br>";
		//$ret = max($array);
		 while (list($key, $va) = each($array)) 
		 {			 
			 $diff = abs($va - $value);			
			 if($tmp == -1)
			 {
				 $tmp = $diff;
				 $k = $key;
				 //echo "1st ".$key." & ".$diff." = ".$value." diff ".$tmp."<br><br><br>";
				
			 }
			 else
			 {
				 //echo $key." & ".$diff." = ".$value." diff ".$tmp."<br>";
				 if($tmp > $diff) 
				 {
					 $tmp = $diff;
					 $k = $key;
				 }
			}
		}
		//echo "<br> ".$key." ".$va." & ".$diff." = ".$value." diff ".$tmp."<br>";
		return $k;
		//echo "<br><br>";
	}
	else 
	{
	  return NULL;
	}
}
function findEqualVal($f,$lv,$hv)
{
	if($f == $lv || $f == $hv)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
function findGoodVal($v,$fstValue,$fstLabel,$cmnValue,$cmnLabel)
{
	if($v == $fstValue)
	{
		$flVal =  $fstLabel;
	}
	elseif($v == $cmnValue)
	{
		$flVal =  $cmnLabel;
	}
	else
	{						
		//echo $diffLabel = $fstLabel - $cmnLabel;
		
		$hVal = $cmnValue;
		$fVal = $fstValue;
		$diff = round(( $fstValue - $cmnValue ) / $cmnLabel, 4);	
		$ct = 0;
		for($k = $cmnLabel; $k<=$fstLabel; $k++)
		{
			$hVal = round($hVal + $diff , 2);
			$ct++;
			
			$tmpAnaVal = findEqualVal($v,$hVal,$fVal);
			if($tmpAnaVal == 1)
			{
				//echo $v.' '.$hVal.' '.$fVal."<br>";
				$flVal = $cmnLabel+$ct;
				break;
				
			}
			else 
			{
				//echo 'ss'.$v;
				$flVal =  $fstLabel;
			}
						
		//$difVal = round( $cmnValue + $diff ,2 );
		//$count++;
		//echo "<br>haf";
		//$diff = round(( $cmnValue - $fstValue ) / $cmnLabel, 4);					
		//echo $flVal =  $cmnLabel + round(( $cmnValue - $v ) / $diff );	
		//echo "<br>";
		}
		//echo "<br>";
	}
	return $flVal;
}
function getFuelPercent($data,$strtTime,$endliveime,$fuel_port,$fLabel,$fValue,$fuel_params)
{
	//echo $strtTime.','.$endliveime.','.$fuel_port.','.$fuel_params.','.$ent_val_mnt;exit;
	$timeArr= array();
	$strTime = '';
	$endTime = '';
	$count = 0;
	$tmpVal = -1;
	
	if(count($data)>0)
	{
		 $data1=explode("#",$data);
		 $j1 = 0;
	 //echo count($data1); 
	  while($j1 < count($data1))
	 {
		$data2=explode("@",$data1[$j1]);
		//echo '<pre>'; print_r($data2);echo '</pre>';
		if(count($data2)>1)
		{
			$data3=explode(",",$data2[1]);
			$vehi=$data3[0];
			$geodate = $data3[8]." ".$data3[9];
			$geoTime = date("H:i:s",strtotime($data3[9]));
			$curTime = explode(":",date("H:i:s",strtotime($data3[9])));
			$newTime  = (($curTime[0]) * 60) + $curTime[1];
			$curTime = (($curTime[0]) * 3600) + ($curTime[1]*60);
			 
			
			if($newTime  >= $strtTime && $newTime <=$endliveime)
			{
			if(!in_array($geoTime,$timeArr))
			{
				$v = splitParameter($fuel_port,$data3[7]);
				if(strlen($v) > 3)			//$fLavel,$fValue
				{
					$v = $v/pow(10,(strlen($v)-1));
					//echo "<br>";
				}
				else
				{
					$v = $v/pow(10,(strlen($v)));
					//echo "<br>";
				}
				
				$v = round($v,2);
				//echo $v.' '.$fValue[2].' '.$fValue[1];
				//echo "<br>";
				//print_r($fLabel);
				if($v >= $fValue[0] && $v <= $fValue[1] )
				{
					//echo "<br>haf";
					//echo $v.' '.$fValue[1].' ';
					//$flVal = findGoodVal($v,$fValue[0],$fLabel[0],$fValue[1],$fLabel[1]);
					$hVal = $fValue[1];
					$fVal = $fValue[0];
					$diff = round(( $fValue[0] - $fValue[1] ) / $fLabel[1], 4);	
					$ct = 0;
					//$eafArr[$fLabel[2]] = $fValue[2];
					for($k = $fLabel[1]; $k<=$fLabel[0]; $k++)
					{
						$eafArr[$k] = $hVal;
						$hVal = round($hVal + $diff , 2);							
					}
					$flVal = getClosestElement($eafArr,$v);
				}
				elseif($v <= $fValue[2] && $v >= $fValue[1] )
				{
					$hVal = $fValue[2];
					$fVal = $fValue[1];
					$diff = round(( $fValue[1] - $fValue[2] ) / $fLabel[1], 4);	
					$ct = 0;
					//$eafArr[$fLabel[2]] = $fValue[2];
					for($k = $fLabel[2]; $k<=$fLabel[1]; $k++)
					{
						$eafArr[$k] = $hVal;
						$hVal = round($hVal + $diff , 2);							
					}
					$flVal = getClosestElement($eafArr,$v);
				}	
				//echo $flVal;
				
				if($strTime == '' && $tmpVal != $flVal)
				{
					$tmpVal = $flVal;					
					$strTime = $geoTime;										
					$count++;
				}
				else if($tmpCount == $flVal && $endTime=='' && $strTime!='')
				{
					$tmpVal = $flVal;
					$count++;
				}
				else if($strTime != '' && $endTime == '' && $tmpCount != $flVal)
				{
					$endTime=$geoTime;
					
					$tmpVal = round($tmpVal / $count);
					$altData [] = $tmpVal.",".$data3[8].",".$strTime.",".$endTime.",".gmdate("H:i:s",(strtotime($endTime) - strtotime($strTime)));
					//echo "<br>";
					$tmpCount = -1;
					$tmpVal = -1;
					$strTime = '';
					$endTime = '';
					$count = 0;
				}
			}
		}
	}
		
	$j1++;
	}
	/*if($strTime != "" && $endTime == "")
	{
		$endTime=$geoTime;
		
		$tmpVal = round($tmpVal / $count);
		$altData [] = $tmpVal.",".$data3[8].",".$strTime.",".$endTime.",".gmdate("H:i:s",(strtotime($endTime) - strtotime($strTime)));
	}*/
	}
	//echo $altData;
	//echo implode("#",$altData)."<br>";
	if(count($altData) > 0)
		return implode("#",$altData);
	else 
		return "";
}

function getFuel($data,$strtTime,$endliveime,$fuel_port,$fuel_params,$ent_val_mnt)
{
	//echo $strtTime.','.$endliveime.','.$fuel_port.','.$fuel_params.','.$ent_val_mnt;exit;
	$timeArr= array();
	$strTime = '';
	$endTime = '';
	$count = 0;
	$tmpVal = -1;
	
	if(count($data)>0)
	{
		 $data1=explode("#",$data);
		 $j1 = 0;
	 //echo count($data1); 
	  while($j1 < count($data1))
	 {
		$data2=explode("@",$data1[$j1]);
		//echo '<pre>'; print_r($data2);echo '</pre>';
		if(count($data2)>1)
		{
			$data3=explode(",",$data2[1]);
			$vehi=$data3[0];
			$geodate = $data3[8]." ".$data3[9];
			$geoTime = date("H:i:s",strtotime($data3[9]));
			$curTime = explode(":",date("H:i:s",strtotime($data3[9])));
			$newTime  = (($curTime[0]) * 60) + $curTime[1];
			$curTime = (($curTime[0]) * 3600) + ($curTime[1]*60);
			 
			
			if($newTime  >= $strtTime && $newTime <=$endliveime)
			{
			if(!in_array($geoTime,$timeArr))
			{
				$v = splitParameter($fuel_port,$data3[7]);
				//if(strlen($v) > 3)
				//{
				$v = $v/pow(10,(strlen($v)-1));
				$flVal = getClosestElement($fuel_params,$v);
					//$altData [] = $tmpVal.",".date("h:i:s A",strtotime($data3[9])).",".$data3[8].",".$strTime.",".$endTime;
				//}
				if($strTime == '' && $tmpVal != $flVal)
				{
					$tmpVal = $flVal;					
					$strTime = $geoTime;										
					$count++;
				}
				else if($tmpCount == $flVal && $endTime=='' && $strTime!='')
				{
					$tmpVal = $flVal;
					$count++;
				}
				else if($strTime != '' && $endTime == '' && $tmpCount != $flVal)
				{
					$endTime=$geoTime;
					
					$tmpVal = round($tmpVal / $count);
					$altData [] = $tmpVal.",".$data3[8].",".$strTime.",".$endTime.",".gmdate("H:i:s",(strtotime($endTime) - strtotime($strTime)));
					//echo "<br>";
					$tmpCount = -1;
					$tmpVal = 0;
					$strTime = '';
					$endTime = '';
					$count = 0;
				}
			}
		}
	}
		
	$j1++;
	}
	if($strTime != "" && $endTime == "")
	{
		$endTime=$geoTime;
		
		$tmpVal = round($tmpVal / $count);
		$altData [] = $tmpVal.",".$data3[8].",".$strTime.",".$endTime.",".gmdate("H:i:s",(strtotime($endTime) - strtotime($strTime)));
	}
	}
	//echo implode("#",$altData)."<br>";
	if(count($altData) > 0)
		return implode("#",$altData);
	else 
		return "";
}

if($recordUserInfo[ci_clientType] == "Client" && $recordUserInfo[ui_isAdmin] == "1")
{
	$devices_query =  "SELECT * FROM tb_deviceinfo,tb_client_subscription WHERE tcs_isActive = 1 AND tcs_deviceId = di_id AND di_clientId =".$_SESSION[clientID]." AND di_status = 1 ORDER BY di_deviceName,di_deviceId ASC";
}
else if($recordUserInfo[ci_clientType] == "Client" && $recordUserInfo[ui_isAdmin] == "0" && $recordUserInfo[ui_roleId] == "1")
{
	$devices_query = "SELECT * FROM tb_deviceinfo,tb_client_subscription WHERE tcs_isActive = 1 AND tcs_deviceId = di_id AND di_status = 1 AND di_clientId=".$_SESSION[clientID]." AND di_assignedUserId = ".$_SESSION[userID]." ORDER BY di_deviceName,di_deviceId ASC";
}
//echo $devices_query;
//$devices_query =  "SELECT * FROM tb_deviceinfo WHERE di_clientId =".$_SESSION[clientID]." AND di_status = 1 ORDER BY di_deviceName,di_deviceId ASC";
$devices_resp = mysql_query($devices_query);	


?>
<script type="text/javascript" language="javascript">

/*function days_between(date1, date2) {

    var ONE_DAY = 1000 * 60 * 60 * 24

    var date1_ms = date1.getTime()
    var date2_ms = date2.getTime()
	var difference_ms = date1_ms - date2_ms
    return Math.round(difference_ms/ONE_DAY)

}*/
var ajax1=new sack();
function days_between(startDate, endDate)
{
    return endDate - startDate;
}
function showFormDiv()
{
	
	if(document.getElementById('formDiv').style.display=='none')
	{
		document.getElementById('formDiv').style.display = 'block';
		document.getElementById('shLink').innerHTML = "Hide Form";
	}
	else
	{
		document.getElementById('formDiv').style.display = 'none';
		if(document.getElementById('shLink'))
		document.getElementById('shLink').innerHTML = "Show Form";
	}
}


function validateMapReport()
{
  if(document.getElementById('selCalcMethod').value== 0 )
  { alert("Select Calculation Method");  document.getElementById('selCalcMethod').focus();  return 0;  }
  
  if(document.getElementById('map_device_id').value== 0 )
  { alert("Select Device");  document.getElementById('map_device_id').focus();  return 0;  }
    
  if(document.getElementById('rdAlertBy').value== 0 && document.getElementById('map_device_id').value != 0)
  { alert("Please Select Input Parameter");  document.getElementById('rdAlertBy').focus();  return 0;  }
  
  if(document.getElementById('rdAlertBy').value== 0 && document.getElementById('map_device_id').value == 0)
  { alert("Please Select Device first");  document.getElementById('rdAlertBy').focus();  return 0;  }
  
  if(document.getElementById('from_date').value=='')
  { alert("Select From Date"); document.getElementById('from_date').focus();  return 0;  }
  
  if(document.getElementById('to_date').value=='')
  { alert("Select To Date");  document.getElementById('to_date').focus(); return 0; }
  
	var t1=document.getElementById('from_date').value ;
        var t2=document.getElementById('to_date').value;

        var one_day=1000*60*60*24; 
		var x=t1.split("-");     
        var y=t2.split("-");
        var date1=new Date(x[2],(x[1]-1),x[0]);
  
        var date2=new Date(y[2],(y[1]-1),y[0]);
        var month1=x[1]-1;
        var month2=y[1]-1;
        var diff=Math.ceil((date2.getTime()-date1.getTime())/(one_day)); 
         
		 if(diff < 0){
       alert("From Date should be prior to To Date."); document.getElementById('to_date').select();return 0;

		 }

	
	return 1;
	
}
function sendCSVData()
{
  document.frmKiloData.submit();
}
function getDeviceDetails(e)
{
	
	document.frm_map_filter.rdAlertBy.selectedIndex = 0;
	document.frm_map_filter.txtVehicleName.value = e.options[e.selectedIndex].text;
	val = e.options[e.selectedIndex].value.split("_");
	document.frm_map_filter.txtVehicleImei.value = val[1]
	
	ajax1.requestFile = 'ajax_server.php?getAlertListforAnalog='+val[0];			
	//alert(ajax1.requestFile)
	ajax1.onCompletion = function(){getAlertList()};
	document.getElementById('rdAlertBy').innerHTML = '</option value=0>Loading...</option>';
	ajax1.runAJAX();
	
}
function getAlertList()
{
	if(ajax1.response)
	{
		var obj = document.getElementById('rdAlertBy');
		
		var selLengh = 0;
		obj.options[0] = new Option("Select Parameter",0);
		selLengh++;
		
		var analogPort1 = ajax1.response;
		analogPort = analogPort1.split("@");
		for(var k=0;k<analogPort.length-1;k++)
		{						
			d1 = analogPort[k].split("=");
			//alert(d1[1]+" "+d1[0])
			//Assign different attributes to the element.
		 
			//var foo = document.getElementById("analogTitle"+(k+1));
		 	//$("analogTitle"+(k+1)).append('<p>Test</p>');
			//Append the element in page (in span).
			//foo.appendChild(element1);
			//var type = '<?php if($_POST[txtLabelName]) echo $_POST[txtLabelName]; else echo '';?>';
			var type = '';
			if(d1[0] != 0)
			{
				if(type == d1[0])
				{
					obj.options[selLengh] = new Option(d1[0], d1[1]);
					obj.options[selLengh].selected = true;
				}
				else
				{
					obj.options[selLengh] = new Option(d1[0], d1[1]);
				}
				selLengh++;
			}	
			
			/*if(vol.length >0 && ltr.length > 0)
			{
				finVal =  getClosestElement(vol, ltr, (fuelValue/1000));
				var n = k+1;
				document.getElementById("fuel"+(k+1)).style.display = "block";
				//var fuelObj = new Fuel("fuel1");
				document.getElementById("analogTitle"+n).className = "thHead";
				document.getElementById("analogTitle"+n).innerHTML = d1[0];
				//alert(document.getElementById("analogTitle"+(k+1)).innerHTML)
				fuelGauge.SetFuel(50);
			}*/
		}	
	
	}

	//removeOptionLast(selLengh)
}
function showPreloader()
{
	var returnVal = validateMapReport()
	if(returnVal == 1)
	{

		document.getElementById('popup_div').innerHTML = '<div id="loading_txt" >Loading...</div>';
		document.frm_map_filter.submit();
	}
}
var minVal;
var m1 = '<?php if($_POST[selDuration]) echo $_POST[selDuration]; else echo 5;?>';
for(k=1; k <= 30; k++)
{
	if(k == m1)
	{
		minVal +='<option value='+k+' selected="selected">'+k+' Min</option>';
	}
	else
	{
		minVal +='<option value='+k+'>'+k+' Min</option>';
	}
}

function hidePreLoader()
{
	document.getElementById('popup_div').innerHTML = '&nbsp;';

}
jQuery(function() {

    //$("#time3, #time4").timePicker();	 onchange="document.frm_map_filter.txtVehicleName.value = this.options[selectedIndex].text"
	 $("#time3, #time4").timePicker({
	  startTime: "12:01 AM", // Using string. Can take string or Date object.
	  endTime: "11:59 PM", // Using Date object here.
	  show24Hours: false,
	  separator: ':',
	  step: 1});    
        
    // Store time used by duration.
    var oldTime = $.timePicker("#time3").getTime();
    
    // Keep the duration between the two inputs.
    $("#time3").change(function() {
      if ($("#time4").val()) { // Only update when second input has a value.
        // Calculate duration.
        var duration = ($.timePicker("#time4").getTime() - oldTime);
        var time = $.timePicker("#time3").getTime();
        // Calculate and update the time in the second input.
        $.timePicker("#time4").setTime(new Date(new Date(time.getTime() + duration)));
        oldTime = time;
      }
    });
    // Validate.
    $("#time4").change(function() {
      if($.timePicker("#time3").getTime() > $.timePicker(this).getTime()) {
        $(this).addClass("error");
      }
      else {
        $(this).removeClass("error");
      }
    });
    
  });



$(function() {
	$( "#from_date" ).datepicker({
		changeMonth: true,
		changeYear: true,
		maxDate: 0,
		showOn: "button",
		buttonImage: "images/calendar.gif",
		buttonImageOnly: true,
		dateFormat : "dd-mm-yy"
	});
});
$(function() {
	$( "#to_date" ).datepicker({
		changeMonth: true,
		changeYear: true,
		maxDate: 0,
		showOn: "button",
		buttonImage: "images/calendar.gif",
		buttonImageOnly: true,
		dateFormat : "dd-mm-yy"
	});
});
</script>
<div id="formDiv">
<form id="frm_map_filter" name="frm_map_filter" method="post" action="" onsubmit="return validateMapReport();"> 
<input type="hidden" name="txtLabelName" id="txtLabelName" />
<table class="gridform_final">
<tr><th colspan="9">Analog Report [Ex: Fuel, Water, etc...]</th></tr>
<tr>
	<td>Select Calculation Method</td>
	<td colspan="8">
		<select name="selCalcMethod" id="selCalcMethod" style="width:80%" tabindex="1" onchange="document.getElementById('map_device_id').selectedIndex = 0">
			<option value="0">Select Method to calculate analog data consumption</option>
			<option value="1">Calculate Based on Calibration Chart Values</option>
			<option value="2">Calculate Based on % Formula</option>
		</select>
	</td>
</tr>
  <tr>
    <td width="20%" align="right"><span class="form_text">Select Device</span>
        <select name="map_device_id" id="map_device_id" tabindex="2" style="width:100%;" onchange="getDeviceDetails(this)">
        <option value="0">Select Device</option>
        <?php 
        while($devices_fetch = @mysql_fetch_assoc($devices_resp)) 
        {
			$renewDate = date("d-m-Y",strtotime("-1 days ".$devices_fetch[tcs_renewalDateTo]));
			//echo $date_offline." <= ".date("d-m-Y",strtotime($renewDate))."<br>";
			if(strtotime(date('d-m-Y')) <= strtotime($renewDate))
			{
			if($devices_fetch[di_deviceName])
				$devName = $devices_fetch[di_deviceName];
			else
				$devName = $devices_fetch[di_deviceId];
        ?>
        <option value="<?php echo $devices_fetch[di_id]."_".$devices_fetch[di_imeiId]; ?>" 
        <?php if($_POST[map_device_id] == $devices_fetch[di_id]."_".$devices_fetch[di_imeiId]) echo "selected"; ?>><?php echo $devName; ?></option>
        <?php }} ?>	
        </select>
        <input type="hidden" name="txtVehicleName" id="txtVehicleName" />
        <input type="hidden" name="txtVehicleImei" id="txtVehicleImei" />
    </td>
     <td width="20%" align="right"><span class="form_text">Select Parameter</span>
        <select name="rdAlertBy" id="rdAlertBy" tabindex="3" style="width:100%" onchange="document.getElementById('txtLabelName').value = this.options[selectedIndex].text">
        	<option value="0">Select Parameter</option>
        </select>
    </td>
	 <!--<td align="right" width="5%"><span class="form_text">Time Diff.</span>
        <select id="selDuration" name="selDuration" tabindex="4">
			<script language="javascript">
				document.write(minVal);
            </script>
        </select>
    </td>-->
    <td width="18%" align="right"><span class="form_text">From&nbsp;</span>
    <input type="text" name="from_date" id="from_date" readonly="true" tabindex="5" size="12" value="<?php if(isset($_POST[from_date])) echo $_POST[from_date]; else echo date("d-m-Y"); ?>" style="width:32%;"/>&nbsp;
    <span><input type="text" name="time3" readonly="true" id="time3" size="7" tabindex="6" value="<?php if($_POST[time3]) echo $_POST[time3]; else echo "12:01 AM";?>" /></span>
    </td>
    <td width="18%" align="right"><span class="form_text">To&nbsp;</span>
    <input type="text" name="to_date" id="to_date" size="12" style="width:32%;" tabindex="7" readonly="true" value="<?php if(isset($_POST[to_date])) echo $_POST[to_date]; else echo date("d-m-Y"); ?>" />&nbsp;
    <span><input type="text" name="time4" readonly="true" id="time4" size="7" tabindex="8" value="<?php if($_POST[time4]) echo $_POST[time4]; else echo "11:59 PM";?>" /></span>
    </td>
    <td width="12%" style="text-align:center">
    <input type="button" name="map_filter_btn"   value="Filter" class="save_btn" tabindex="9"  onclick="showPreloader();"/>
    <input type="hidden" name="map_filter_btn" value="Filter" />
    <input type="button" name="map_cancel_btn" id="map_cancel_btn" value="Reset" class="save_btn" onclick="location.href='index.php?ch=analogReport';" tabindex="10" /> 
    </td>
  </tr>
  
</table>
</form>
</div>
<div id="popup_div" style=" display:block; border:0px;" >

</div>	
	
<?php
if(isset($_POST[map_filter_btn]) && $_POST[map_filter_btn]!='')
{
 
	//echo '<pre>'; print_r($_POST);echo '</pre>';
	//exit;
	$sdate = $_POST[from_date];
	$edate = $_POST[to_date];
	
	$strtTime = explode(":",date("H:i",strtotime($_POST[time3])));
	$strtTime = (($strtTime[0] * 60) + $strtTime[1]);
	
	$endTime = explode(":",date("H:i",strtotime($_POST[time4])));
	$endTime = (($endTime[0] * 60) + $endTime[1]);
	
	$ent_val_mnt = $_POST[selDuration]*60;
/********End Full tank filled status*********************/
	$d1[1] = $_POST[rdAlertBy];
//	echo strpos($d1[1],"("); exit;
	$d2 = substr($d1[1],0,strpos($d1[1],"("));
	$n = strpos($d1[1],"(")+1;
	$d3 = str_replace(")","",substr($d1[1],$n));
	$d3 = explode("#",$d3);
	$fuel_port = $d2;
	//var fuelValue = getDataFromParam(str,$d2);
	//var vol =  new Array();
	
	//print_r($fuel_params);
	
	if($_POST[selCalcMethod] == 1)
	{
		//var ltr = new Array();
		for($l = 0; $l < count($d3)-1; $l++)
		{
			$d4 = explode("_",$d3[$l]);
			$fuel_params[$d4[0]] = $d4[1];
			//vol[l]=d4[1];
			//ltr[l]=d4[0];
		}
		if($_POST[from_date] == $_POST[to_date])
		{
			$data =  gpspathFunAll($_POST[from_date],$_POST[txtVehicleImei]);		
			$res = getFuel($data,$strtTime,$endTime,$fuel_port,$fuel_params,$ent_val_mnt);
			if($res)
				$finData = $res."#";
			//$finData[] = array(date('d-m-Y',strtotime($_POST[from_date]))=>$data);
		}
		else
		{
			$z = GetDays($sdate, $edate);
			for($y=0; $y<count($z); $y++)
			{ 
				if($y == 0) 
				{
					$strtTime1 = $strtTime;
					$endTime1 = 1439;
					$data =  gpspathFunAll($z[$y],$_POST[txtVehicleImei]);	
					$res1 = getFuel($data,$strtTime1,$endTime1,$fuel_port,$fuel_params,$ent_val_mnt);
					if($res1)		
					$finData[]= $res1;	
					//$finData[] = array(date('d-m-Y',strtotime($z[$y]))=>$data);
				}
				elseif($y == count($z)-1) 
				{
					$strtTime1 = 0;
					$endTime1 = $endTime;
					$data =  gpspathFunAll($z[$y],$_POST[txtVehicleImei]);		
					$res2= getFuel($data,$strtTime1,$endTime1,$fuel_port,$fuel_params,$ent_val_mnt);
					if($res2)		
					$finData[]= $res2;	
					//$finData[] = array(date('d-m-Y',strtotime($z[$y]))=>$data);
				}
				elseif($y < count($z)-1) 
				{
					$strtTime1 = 0;
					$endTime1 = 1439;
					$data =  gpspathFunAll($z[$y],$_POST[txtVehicleImei]);			
					$res3 = getFuel($data,$strtTime1,$endTime1,$fuel_port,$fuel_params,$ent_val_mnt);
					if($res3)		
					$finData[]= $res3;
					//$finData[] = array(date('d-m-Y',strtotime($z[$y]))=>$data);
				}
			}
		
			$finData = implode("#",$finData);
		}
	}
	elseif($_POST[selCalcMethod] == 2)
	{
		//var ltr = new Array();
		for($l = 0; $l < count($d3)-1; $l++)
		{
			$d4 = explode("_",$d3[$l]);
			$fuel_params[$d4[0]] = $d4[1];
			$fuel_label[] = $d4[0];
			$fuel_value[] = $d4[1];
			//vol[l]=d4[1];
			//ltr[l]=d4[0];
		}
		//print_r($fuel_label);
		if($_POST[from_date] == $_POST[to_date])
		{
			$data =  gpspathFunAll($_POST[from_date],$_POST[txtVehicleImei]);		
			$res = getFuelPercent($data,$strtTime,$endTime,$fuel_port,$fuel_label,$fuel_value,$fuel_params);
			if($res)
				$finData = $res."#";
			//$finData[] = array(date('d-m-Y',strtotime($_POST[from_date]))=>$data);
		}
		else
		{
			$z = GetDays($sdate, $edate);
			for($y=0; $y<count($z); $y++)
			{ 
				if($y == 0) 
				{
					$strtTime1 = $strtTime;
					$endTime1 = 1439;
					$data =  gpspathFunAll($z[$y],$_POST[txtVehicleImei]);	
					$res1 = getFuel($data,$strtTime1,$endTime1,$fuel_port,$fuel_params,$ent_val_mnt);
					if($res1)		
					$finData[]= $res1;	
					//$finData[] = array(date('d-m-Y',strtotime($z[$y]))=>$data);
				}
				elseif($y == count($z)-1) 
				{
					$strtTime1 = 0;
					$endTime1 = $endTime;
					$data =  gpspathFunAll($z[$y],$_POST[txtVehicleImei]);		
					$res2= getFuel($data,$strtTime1,$endTime1,$fuel_port,$fuel_params,$ent_val_mnt);
					if($res2)		
					$finData[]= $res2;	
					//$finData[] = array(date('d-m-Y',strtotime($z[$y]))=>$data);
				}
				elseif($y < count($z)-1) 
				{
					$strtTime1 = 0;
					$endTime1 = 1439;
					$data =  gpspathFunAll($z[$y],$_POST[txtVehicleImei]);			
					$res3 = getFuel($data,$strtTime1,$endTime1,$fuel_port,$fuel_params,$ent_val_mnt);
					if($res3)		
					$finData[]= $res3;
					//$finData[] = array(date('d-m-Y',strtotime($z[$y]))=>$data);
				}
			}
		
			$finData = implode("#",$finData);
		}
	}
?>
<table cellpadding="0" cellspacing="0" border="0" class="gridform_final" id="example">      
<thead>
	<tr>
    	<th width="5%">#</th>
	  	<th width="15%">Date (dd-mm-yy) </th>
        <th width="30%">Device Name</th>
        <th width="15%">From Time </th>
		<th width="15%">To Time </th>
		<th width="7%">Duration</th>
        <th width="20%">Fuel (Ltrs)</th>   		
     </tr>
  </thead>
  <tbody>
      
<?php
	if(count($finData) > 0)
	{
		$tTdiff1 = explode("#",$finData);
		//print_r($tTdiff1); //exit;
		for($k=0;$k<count($tTdiff1)-1;$k++)
		{
		$tTdiff = explode(",",$tTdiff1[$k]);
		//print_r($tTdiff); exit;
		?>		
		 <tr <?php if($r % 2 == 0) { echo 'class="odd_row"';} else { echo 'class="even_row"'; } ?> >
		   <td valign="top"><?php echo $k+1; ?></td>
   		   <td valign="top"><?php echo $tTdiff[1]; ?></td>
		   <td valign="top"><?php echo $_POST[txtVehicleName]; ?></td>
		   <td valign="top"><?php echo date('h:i:s A',strtotime($tTdiff[2])); ?></td>
		   <td valign="top"><?php echo date('h:i:s A',strtotime($tTdiff[3])); ?></td>
		   <td valign="top"><?php echo date("H:i:s",strtotime($tTdiff[4])); ?></td>
		   <td valign="top"><?php echo $tTdiff[0]; ?></td>
		 </tr>
		<?php 
		}
	}
?>
</tbody>
</table>
<script type="text/javascript">
//showFormDiv();
hidePreLoader();
</script>
<?php 
}?> 
